<?php
/**
 * AjaxController class.
 * AjaxController is the controller for business portal.
 * 
 * It used for all ajax request from client.
 * 
 * Yawii Business 版权所有 @ 2012.
 */
class AjaxController extends Controller
{
	public function actionPush() {
		$key = Yii::app()->request->getPost('key');
		$val = Yii::app()->request->getPost('val');
		$this->renderPartial('_json', array('model' => $val));
		BUtils::Logging($key, $val);
	}

	/**
	 * Update timezone based on browser.
	 */
	public function actionDebug() {
		$do = Yii::app()->request->getQuery('do', 'show');
		if ($do == 'show') {
			$model = array(
				'session' => $_SESSION,
				'cookie' => $_COOKIE,
			);
		} else {
			session_destroy();
			$model = array(
				'session' => $_SESSION,
				'cookie' => $_COOKIE,
			);
		}
		
		if (Yii::app()->request->isAjaxRequest)
			$this->renderPartial('index', array('model' => $model));
		else
			$this->renderPartial('index', array('model' => $model));
	}
	
	/**
	 * Upload with AJAX 
	 */
	public function actionUpload()
	{
		Yii::import("ext.EAjaxUpload.qqFileUploader");
		$folder = Yii::app()->basePath . '/../upload/'; // folder for uploaded files
		$prefix = Yii::app()->request->getParam('prefix', null);
		$allowedExtensions = array("jpg","jpeg","gif","png");//array("jpg","jpeg","gif","exe","mov" and etc...
		$sizeLimit = 8 * 1024 * 1024;	// maximum file size in bytes
		$uploader = new qqFileUploader($allowedExtensions, $sizeLimit);
		$result = $uploader->handleUpload($folder, $prefix . uniqid());
		$result = htmlspecialchars(json_encode($result), ENT_NOQUOTES);
		echo $result;
	}

	// Uncomment the following methods and override them if needed
	/*
	public function filters()
	{
		// return the filter configuration for this controller, e.g.:
		return array(
			'inlineFilterName',
			array(
				'class'=>'path.to.FilterClass',
				'propertyName'=>'propertyValue',
			),
		);
	}

	public function actions()
	{
		// return external action classes, e.g.:
		return array(
			'action1'=>'path.to.ActionClass',
			'action2'=>array(
				'class'=>'path.to.AnotherActionClass',
				'propertyName'=>'propertyValue',
			),
		);
	}
	*/
}